package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
	"law_case_api/services"
	"law_case_api/utils"
	"net/http"
	"strconv"
	"strings"
	"time"
)

// @Summary 添加案件进度事项
// @Tags 案件管理
// @Accept plain
// @Produce plain
// @Param CreateCaseSchedule body dto.CaseScheduleReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/schedule/create [post]
func CreateCaseSchedule(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	caseScheduleReq := dto.CaseScheduleReq{}
	if err := c.Bind(&caseScheduleReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	if caseScheduleReq.CaseId == 0 {
		resp.Code = 400
		resp.Message = "案件编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	if len(strings.Trim(caseScheduleReq.ScheduleContent, "")) == 0 {
		resp.Code = 400
		resp.Message = "进度事项不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	if len(strings.Trim(caseScheduleReq.ScheduleTime, "")) == 0 {
		resp.Code = 400
		resp.Message = "进度日期不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	wzjCaseSchedule := models.WzjCaseSchedule{}
	_ = utils.MapTo(caseScheduleReq, &wzjCaseSchedule)
	wzjCaseSchedule.CreateAt = time.Now().Unix()
	wzjCaseSchedule.CreateUserId = int64(userid)
	wzjCaseSchedule.CreateUser = claims.UserName

	_case := services.CaseService{}
	if wzjCaseSchedule.ScheduleId > 0 {
		boolRes, err := _case.UpdateCaseSchedule(wzjCaseSchedule)
		if err == nil && boolRes {
			resp.Message = "更新案件进度事项成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("更新案件进度事项失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "更新案件进度事项失败"
		return c.JSON(http.StatusOK, resp)

	} else {
		boolRes, err := _case.CreateCaseSchedule(wzjCaseSchedule)
		if err == nil && boolRes {
			resp.Message = "创建案件进度事项成功"
			return c.JSON(http.StatusOK, resp)
		}
		log.Error("创建案件进度事项失败：", err) //增加日志信息
		resp.Code = 400
		resp.Message = "创建案件进度事项失败"
		return c.JSON(http.StatusOK, resp)
	}
}

// @Summary 获取当前案件条件的信息
// @Tags 案件管理
// @Accept plain
// @Produce plain
// @Param CaseScheduleList body dto.CaseSchedulesListReq true " "
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/schedule/list [get]
func CaseScheduleList(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	caseSchedulesListReq := dto.CaseSchedulesListReq{}
	if err := c.Bind(&caseSchedulesListReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	if caseSchedulesListReq.CaseId == 0 {
		resp.Code = 400
		resp.Message = "案件编号不能为空"
		return c.JSON(http.StatusOK, resp)
	}
	_case := services.CaseService{}
	respData, respPageCount, err := _case.GetCaseScheduleListByCondition(caseSchedulesListReq)
	if err != nil {
		resp.Code = 400
		return c.JSON(http.StatusOK, resp)
	}
	resp.PageCount = respPageCount
	var data []dto.CaseScheduleReq
	utils.MapTo(respData, &data)
	resp.Data = data

	return c.JSON(http.StatusOK, resp)
}
